package cloud.pangui.core.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther hepangui
 * @Date 2020/11/5 0005
 */
public class StringUtils {

	/**
	 * 空字符判断
	 *
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || "".equals(str) || "".equals(str.trim());
	}

	/**
	 * 非空判断
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 * null转空字符串
	 *
	 * @param str
	 * @return
	 */
	public static String nullToEmpty(String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	/**
	 * 空字符串转默认值
	 *
	 * @param str
	 * @return
	 */
	public static String emptyToDefault(String str, String def) {
		if (isEmpty(str)) {
			return def;
		}
		return str;
	}

	/**
	 * 前置填充，
	 * 例如 num =1
	 * 空位为0
	 * 总长为10
	 * 结果为0000000001
	 *
	 * @param num
	 * @param c
	 * @param length
	 * @return
	 */
	public static String fillEmpty(long num, char c, int length) {
		String numStr = String.valueOf(num);
		int num_len = numStr.length();
		if (num_len >= length) {
			return numStr.substring(num_len - length);
		}
		StringBuilder sb = new StringBuilder(length);
		while (sb.length() < length - num_len) {
			sb.append(c);
		}
		sb.append(numStr);
		return sb.toString();
	}

	/**
	 * 去掉末尾字符串
	 */
	public static String removeEnd(String str, String endStr) {
		if (str == null) {
			return str;
		}
		if (!str.endsWith(endStr)) {
			return str;
		}
		return str.substring(0, str.lastIndexOf(endStr));
	}

	/**
	 * 去掉首字符串
	 */
	public static String removeStart(String str, String startStr) {
		if (str == null) {
			return str;
		}
		if (!str.startsWith(startStr)) {
			return str;
		}
		return str.substring(str.indexOf(startStr));
	}

	/**
	 * 分隔字符串
	 *
	 * @param str
	 * @param split
	 * @return
	 */
	public static List<String> split(String str, String split) {
		String[] split1 = str.split(split);
		List list = new ArrayList();
		for (String s : split1) {
			list.add(s);
		}
		return list;
	}

	/**
	 * 集合转字符串
	 *
	 * @param list
	 * @param split
	 * @return
	 */
	public static String join(Collection<String> list, String split) {
		if (list == null || list.size() == 0) {
			return "";
		}
		if (split == null) {
			split = ",";
		}
		StringBuilder sb = new StringBuilder();
		for (String s : list) {
			sb.append(s).append(split);
		}
		return removeEnd(sb.toString(), split);
	}

	/**
	 * 数组join成字符串
	 *
	 * @param array
	 * @param split
	 * @return
	 */
	public static String join(String[] array, String split) {
		if (array == null || array.length == 0) {
			return "";
		}
		if (split == null) {
			split = ",";
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < array.length; i++) {
			sb.append(array[i]);
			if (i != array.length - 1) {
				sb.append(split);
			}
		}
		return sb.toString();
	}

	/**
	 * 寻找出符合起始标志和结束标志的字串组
	 *
	 * @param str       字符串源
	 * @param startSign 起始标志
	 * @param endSign   结束标志
	 */
	public static List findPairs(String str, String startSign, String endSign) {
		List l = new LinkedList();
		int c = 0;
		while ((c = str.indexOf(startSign, c + 1)) >= 0) {
			int ce = str.indexOf(endSign, c);
			if (ce > 0) l.add(str.substring(c, ce) + endSign);
		}
		return l;
	}

	/**
	 * 寻找匹配项,返回List
	 *
	 * @param str   字符串
	 * @param regex 正则式
	 */
	public static List<String> findMatches(String str, String regex) {
		List<String> l = new LinkedList<String>();
		Matcher m = Pattern.compile(regex).matcher(str);
		while (m.find()) {
			l.add(m.group());
		}
		return l;
	}

}
